package com.lsk.staff;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.lsk.util.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018-11-20.
 */
public class StaffServices{
    Cache cache= Redis.use(PropKit.get("cache.name"));
    public Map staffListService(int page, int size){
        Map map=new HashMap();
        String sql="SELECT * from staff ";
        PageUtil pageUtil=new PageUtil(page,size,sql);
        int count=pageUtil.getTotalRows();
        map.put("code",0);
        if (count>0){
            map.put("count",count);
            map.put("data",pageUtil.getList());
        }else {
            map.put("count",0);
            map.put("msg","暂无数据");
        }
        return map;
    }
    /**
     * 获取员工详情
     *
     */
    public Record getStaffInfo(String id){
        String sql="SELECT *from staff where id='"+id+"'";
        Record record= Db.findFirst(sql);
        return record;
    }

    /**
     * 保存用户信息
     */
    public ReturnData saveStaffInfoServce(Map<String,String[]> map){
        ReturnData returnData=new ReturnData();
        boolean result=false;
        String name=map.get("name")[0].trim();
        String tel=map.get("tel")[0].trim();
        String idCard=map.get("idCard")[0].trim();
        String address=map.get("address")[0].trim();
        String id=map.get("id")[0].trim();
        String number=map.get("number")[0].trim();
        String userId=map.get("userId")[0].trim();
        Record record=new Record().set("name",name).set("tel",tel).set("idCard",idCard)
                .set("address",address).set("number",number);
        if (id==null || "".equals(id) || "null".equals(id)){//新添加的员工
            /*先去用户表中查询该手机号对应的员工*/
            String userSql="SELECT *from sys_user where userCode='"+tel+"'";
            Record user=Db.findFirst(userSql);
            if (user!=null){//该手机号已有对应的账户
                returnData.setData("该手机号已有对应的账户");
                returnData.setResult(false);
            }else {//创建员工
                userId=UtilUUID.getUUID();
                record.set("id", UtilUUID.getUUID()).set("userId",userId);
                result=Db.save("staff","id",record);
                if (result) {//操作成功
                    returnData.setResult(true);
                    /*创建用户*/
                    String salt = UtilUUID.getOrderIdByUUId();
                    Record userRecord = new Record();
                    userRecord.set("id", userId).set("locked", "0").set("type", "1");
                    userRecord.set("password", MD5.md5(tel + salt + "123456")).set("userName", name).set("salt", salt).set("userCode", tel)
                            .set("phone", tel);
                    boolean saveUser = Db.save("sys_user", "id", userRecord);
                    if (saveUser) {//用户保存成功
                        returnData.setResult(true);
                        returnData.setMsg("保存员工成功");
                    } else {
                        throw new RuntimeException();//抛出异常事务回滚
                    }
                }else {
                    throw new RuntimeException();//抛出异常事务回滚
                }
            }
        }else {//修改员工信息
            result=Db.update("staff","id",record);
            String salt=UtilUUID.getOrderIdByUUId();
            Record userRecord = new Record();
            userRecord.set("id", userId).set("phone", tel);
            boolean upUser=Db.update("sys_user","id",userRecord);
            if (result==false || upUser==false){//抛出异常
                throw new RuntimeException();
            }else {
                returnData.setResult(true);
                returnData.setMsg("保存成功");
            }
        }
        return returnData;
    }
}
